home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 20 / Cream of the Crop 20 (Terry Blount) (1996).iso / utility / freedos.zip / COM050.ZIP / REDIR.C < prev    next >
C/C++ Source or Header  |  1996-01-17  |  2KB  |  84 lines

  1. /*
  2.  *  REDIR.C
  3.  *
  4.  *
  5.  *
  6.  *  Comments:
  7.  *
  8.  *  12/15/95 (Tim Norman) ---------------------------------------------------
  9.  *    started.
  10.  *
  11.  */
  12.  
  13. #include <stdio.h>
  14. #include <ctype.h>
  15. #include <string.h>
  16.  
  17. int is_redir (char c)
  18. {
  19.    return c == '<' || c == '>' || c == '|';
  20. }
  21.  
  22. /* gets the redirection from the command line and chops it out of the */
  23. /* command line                                                       */
  24. void get_redirection (char *s, char *in, char *out, char *pipe[128], int *num)
  25. {
  26.    int count, start, inquote = 0, numpipes = 0;
  27.  
  28.    /* find and remove all the redirections first */
  29.    for (count = 0; s[count]; count++)
  30.       if (s[count] == '"')
  31.      inquote = !inquote;
  32.       else if (!inquote && (s[count] == '<' || s[count] == '>'))
  33.       {
  34.      /* MS-DOS ignores multiple redirection symbols and uses the last */
  35.      /* redirection, so we'll emulate that and not check */
  36.  
  37.      /* find the next word */
  38.      start = count;
  39.      count++;
  40.  
  41.      /* skip over whitespace */
  42.      while (isspace (s[count]))
  43.         count++;
  44.  
  45.      /* skip until we hit whitespace or a delimiter of some sort */
  46.      while (!is_redir (s[count]) && s[count] && !isspace (s[count]))
  47.         count++;
  48.  
  49.      if (s[start] == '<')
  50.      {
  51.         memcpy (in, &s[start+1], count - start - 1);
  52.         in[count - start - 1] = 0;
  53.      }
  54.      else if (s[start] == '>')
  55.      {
  56.         memcpy (out, &s[start+1], count - start - 1);
  57.         out[count - start - 1] = 0;
  58.      }
  59.  
  60.      memmove (&s[start], &s[count], strlen (&s[count]) + 1);
  61.  
  62.      /* move back one so we can check this character */
  63.      count = start - 1;
  64.       }
  65.  
  66.    /* now go after the pipes */
  67.  
  68.    pipe[numpipes++] = s;
  69.  
  70.    for (count = 0; s[count]; count++)
  71.       if (s[count] == '"')
  72.      inquote = !inquote;
  73.       else if (!inquote && s[count] == '|')
  74.       {
  75.      s[count] = 0;
  76.  
  77.      pipe[numpipes++] = &s[count + 1];
  78.       }
  79.  
  80.    pipe[numpipes] = NULL;
  81.  
  82.    *num = numpipes;
  83. }
  84.